package com.lyf.controll;

import cn.hutool.json.JSONObject;
import com.lyf.pojo.User;
import com.lyf.result.Code;
import com.lyf.result.Result;
import com.lyf.service.UserService;
import com.lyf.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/register")
public class RegisterControll extends HttpServlet {
    UserService userService = new UserServiceImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        username,loginname,password,img,phone,createdate
        String username = req.getParameter("username");
        String loginname = req.getParameter("loginname");
        String password = req.getParameter("password");
        String img = req.getParameter("img");
        String phone = req.getParameter("phone");
        // 获取当前时间
        LocalDateTime createdate = LocalDateTime.now();

        User user = new User();
        user.setUsername(username);
        user.setLoginname(loginname);
        user.setPassword(password);
        user.setImg(img);
        user.setPhone(phone);
        user.setCreateDate(createdate);

        try{
            userService.register(user);
            Map<String,Object> map = new HashMap<>();
            Result result = new Result(Code.OK,null,"注册成功！");
            JSONObject jo = new JSONObject();

            map.put("result",result);
            jo.putAll(map);
            //响应到浏览器
            resp.getWriter().println(jo.toString());
        }catch (Exception e){
            e.printStackTrace();
        }

    }
}
